import React, { Component } from 'react';
import { Table } from 'antd';
import { connect } from 'dva';
import styles from './style.less';

@connect(({ loading, payapply }) => ({
  loading: loading.effects['payapply/getSecond'],
  detailData: payapply.detailData,
  payapply,
}))
class StaticTable extends Component {
  componentDidMount() {
    const { payapply, businessData } = this.props;
    this.handlePaginationData({
      data: businessData,
      current: payapply.staticPaginationData.pagination.current,
      pageSize: payapply.staticPaginationData.pagination.pageSize,
    });
  }

  componentWillUnmount() {
    const { dispatch } = this.props;
    dispatch({
      type: 'payapply/updateModelData',
      payload: {
        staticPaginationData: {
          list: [],
          pagination: {
            current: 1,
            pageSize: 10,
            total: 10,
          },
        },
        businessData: [],
      },
    });
  }

  handleTableChange = pagination => {
    const { businessData } = this.props;
    this.handlePaginationData({
      data: businessData,
      current: pagination.current,
      pageSize: pagination.pageSize,
    });
  };

  handlePaginationData = params => {
    const { dispatch } = this.props;
    if (!params.data.length) return;
    dispatch({
      type: 'payapply/handlePaginationData',
      payload: {
        data: params.data,
        current: params.current,
        pageSize: params.pageSize,
      },
    });
  };

  render() {
    const {
      payapply: { staticPaginationData },
      rowKey,
      loading,
      columns,
    } = this.props;
    const paginationProps = {
      showSizeChanger: true,
      showQuickJumper: true,
      showTotal: total => {
        const totalPage =
          total < staticPaginationData.pagination.pageSize
            ? 1
            : Math.ceil(total / staticPaginationData.pagination.pageSize);
        return `共 ${total} 条记录 第 ${staticPaginationData.pagination.current} / ${totalPage}页`;
      },
      ...staticPaginationData.pagination,
    };

    return (
      <div className={styles.standardTable}>
        <Table
          loading={loading}
          dataSource={staticPaginationData.list}
          rowKey={rowKey}
          columns={columns}
          rowSelection={null}
          pagination={paginationProps}
          onChange={this.handleTableChange}
        />
      </div>
    );
  }
}

export default StaticTable;
